package com.campus.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campus.commons.entity.Class;
import com.campus.app.mapper.ClassMapper;
import com.campus.app.service.ClasssService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.commons.utils.Result;
import com.campus.commons.utils.Tool;
import com.campus.config.lifecycle.CustomInit;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 班级表 服务实现类
 * </p>
 *
 * @author Rin
 * @since 2020-12-24
 */
@Service
public class ClassServiceImpl extends ServiceImpl<ClassMapper, Class> implements ClasssService {
    @Autowired
    private ClassMapper classMapper;

    @Override
    public Result<List<Class>> getClassByTeacherId(Integer teacher) {
        QueryWrapper<Class> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacherId", teacher).or().eq("lifeTeacherId",teacher);
        return Result.result(classMapper.selectList(queryWrapper));
    }

    @Override
    public Result<List<Class>> getAllClass() {
        return Result.result(classMapper.selectList(filterIsOver(null,false)));
    }

    private QueryWrapper filterIsOver(QueryWrapper queryWrapper,boolean isOver) {
        if (queryWrapper==null) {
            queryWrapper = new QueryWrapper();
        }
        queryWrapper.eq("isOver", isOver);
        return queryWrapper;
    }
    @Override
    public Boolean exitClassByName(String className) {
        QueryWrapper<Class> wrapper = new QueryWrapper<>();
        wrapper.eq("className", className);
        Integer count = classMapper.selectCount(wrapper);
        return count==1;
    }

    @Override
    public Result<List<Class>> getAlls(int page, int limit) {
        return Result.result(classMapper.selectPage(new Page<Class>(page, limit), null));
    }

    @Override
    public Result deleteClass(int id) {
        Result result = new Result();
        int delete = classMapper.delClassByid(id);
        if (delete > 0) {
            Class.classes.remove(id);
            result.setSuccess(true);
            return result;
        }
        result.setSuccess(false);
        return result;
    }

    @Override
    public Result<List<Class>> getAllBy(String begin, String end, int page, int limit, Class c) {

        QueryWrapper<Class> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(begin) && StringUtils.isEmpty(end)) {
            wrapper.ge("creatTime", begin);
        }

        if (StringUtils.isEmpty(begin) && !StringUtils.isEmpty(end)) {
            wrapper.ge("creatTime", end);
        }
        if (!StringUtils.isEmpty(begin) && !StringUtils.isEmpty(end)) {
            wrapper.between("creatTime", begin, end);
        }
        if (!StringUtils.isEmpty(c.getClassname())) {
            wrapper.like("className", c.getClassname());
        }
        if (!StringUtils.isEmpty(c.getSemesterName())) {
            wrapper.inSql("semesterId", "SELECT id FROM semester  WHERE semesterName like '%" + c.getSemesterName() + "%'");
        }
        if (!StringUtils.isEmpty(c.getTeacherName())) {
            wrapper.inSql("teacherId", "SELECT id FROM teacher WHERE teacherName like '%" + c.getTeacherName() + "%'");
        }
        if (!StringUtils.isEmpty(c.getLifeTeacherName())) {
            wrapper.inSql("lifeTeacher", "SELECT id FROM teacher WHERE teacherName like '%" + c.getLifeTeacherName() + "%'");
        }

        return Result.result(classMapper.selectPage(new Page<Class>(page, limit), wrapper));

    }

    @Override
    public Result<Class> getOne(int id) {
        QueryWrapper<Class> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        return Result.verify(classMapper.selectOne(queryWrapper));
    }

    @Autowired
    private CustomInit customInit;
    @Override
    public Result update_xml(Class c) {

        new FunctionServerImpl().init();
        c.setTeacherid(FunctionServerImpl.teachers.get(c.getTeacherName()));
        c.setLifeteacherid(FunctionServerImpl.teachers.get(c.getLifeTeacherName()));
        String date = Tool.getDateToString(c.getCreattime());
        int xml = classMapper.updateClass_xml(c.getId(), c.getClassname(), date, c.getSemesterName(), c.getStipulate(), c.getTeacherid(), c.getLifeteacherid());
        if (xml == 1) {
            customInit.setClass();
        }

        return Result.result(xml);
    }

    @Autowired
    private SemesterServiceImpl semesterService;
    @Override
    public Result addClass(Class c) {
        new FunctionServerImpl().init();
        c.setTeacherid(FunctionServerImpl.teachers.get(c.getTeacherName()));
        c.setLifeteacherid(FunctionServerImpl.teachers.get(c.getLifeTeacherName()));
        String  date = Tool.getDateToString(c.getCreattime());
        int semesterid = semesterService.getSemesterid(c.getSemesterName());
        int count = classMapper.addClass_xml(c.getClassname(), date, semesterid, c.getStipulate(), c.getTeacherid(), c.getLifeteacherid());
        if (count == 1) {
            customInit.setClass();

        }
        return Result.result(count);
    }
}
